package com.ruoyi.web.controller.blog.api;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.blog.service.BlogService;
import com.ruoyi.blog.service.IWebTagService;
import com.ruoyi.common.annotation.Check;
import com.ruoyi.common.annotation.RequestLock;

import com.ruoyi.common.constant.ActivitysConstant;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.model.blog.Blog;
import com.ruoyi.model.blog.dto.BlogDto;

import com.ruoyi.model.user.UserInfo;

import com.ruoyi.ucenter.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 文章Controller
 *
 * @author ruoyi
 * @date 2024-04-03
 */
@Api(tags = "前台:文章接口")
@RestController
@RequestMapping("/api/blog")
public class BlogController extends BaseController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private IUserInfoService iUserInfoService;

    @Autowired
    private IWebTagService webTagService;

    @Autowired
    private RedisCache redisCache;


    @ApiOperation("查询所有的文字标签")
    @GetMapping("/tags")
    public AjaxResult queryTagList() {
        return success(webTagService.list());
    }

    /**
     * 根据推送ID查询索引博客列表。
     */
    @GetMapping("/list/{pushId}")
    public TableDataInfo queryIndexBlogs(@PathVariable("pushId") String pushId) {
        startPage();
        List<Blog> blogs = blogService.selectBlogByPushId(pushId);
        return getDataTable(blogs);
    }

    /**
     * 获取文章详细信息
     */
    @ApiOperation(value = "查询文章详情", notes = "并且返回对应的评论列表,并且增加对应的浏览量")
    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        Blog bgInfo = this.blogService.getById(id);
        UserInfo userInfo = iUserInfoService.getById(bgInfo.getPushId());
        bgInfo.setPushName(
                userInfo != null ? userInfo.getNickName() : null
        );
        bgInfo.setPushId(null);
        //5.新增浏览量
        redisCache.incrementCacheMapValue(ActivitysConstant.BLOG_KEY, bgInfo.getId(), 1);
        return AjaxResult.success(bgInfo);
    }

    /**
     * 获取文章详细信息
     */
    @ApiOperation(value = "查询文章列表")
    @GetMapping(value = "/list")
    public AjaxResult getList() {
        List<Blog> list = blogService.list(Wrappers.<Blog>lambdaQuery().eq(Blog::getStatus, "0").orderByDesc(Blog::getPushTime));
        return AjaxResult.success(list.stream().peek(item -> item.setPushName(this.iUserInfoService.getById(item.getPushId()) != null
                ? this.iUserInfoService.getById(item.getPushId()).getNickName()
                : null)).collect(Collectors.toList()));
    }


    @ApiOperation("发布文章")
    @Check
    @PostMapping("/push/blog")
    @RequestLock(prefix = "pushBlog", expire = 1, timeUnit = TimeUnit.SECONDS)
    public AjaxResult pushActivity(@ApiParam("发布活动信息") @RequestBody BlogDto blogDto) {
        return toAjax(blogService.pushBlog(blogDto));
    }


    /**
     * 删除文章
     */
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(blogService.deleteWebBlogByIds(ids));
    }

    /**
     * 查询文章详情
     */
    @Check
    @GetMapping("/getActivityInfo/{id}/{type}")
    public AjaxResult getContestInfo(@ApiParam("文章Id") @PathVariable("id") String blogId,
                                     @ApiParam("类型") @PathVariable String type) {
        return success(this.blogService.getBlogAndCommonVo(blogId, type));
    }

    /**
     * 修改文章
     */
    @PreAuthorize("@ss.hasPermi('system:blog:edit')")
    @Log(title = "修改文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogDto blogDto) {
        return toAjax(blogService.updateBlog(blogDto));
    }


}

